package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.asn1.axdr.AxdrSequenceOf;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

import java.util.List;

/**
 * DataSequence类
 * 实现数据序列的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class DataSequence extends AxdrSequenceOf<Data> {

    private byte[] dataCode = null;

    /**
     * 默认构造函数
     */
    public DataSequence() {
        super();
    }

    /**
     * 设置数据编码
     * 
     * @param dataCode 数据编码
     */
    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    /**
     * 设置序列
     * 
     * @param dataList 数据列表
     */
    public void setSequence(List<Data> dataList) {
        super.setSeq(dataList);
    }

    /**
     * 获取序列大小
     * 
     * @return 序列大小
     */
    public int size() {
        return super.size();
    }

    /**
     * 获取指定索引的数据
     * 
     * @param index 索引
     * @return 数据
     */
    public Data get(int index) {
        return super.get(index);
    }

    /**
     * 添加数据
     * 
     * @param data 数据
     */
    public void add(Data data) {
        super.add(data);
    }

    @Override
    protected Data createListElement() {
        return new Data();
    }

    /**
     * 编码并保存
     * 
     * @param encodingSizeGuess 编码大小预估
     */
    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            super.encode(revOStream);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("Error encoding DataSequence", e);
        }
        this.dataCode = revOStream.getArray();
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        return super.decode(input);
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        return super.encode(output);
    }

    @Override
    public String toString() {
        return "DataSequence: " + super.toString();
    }
}